//
//  ScanViewController.m
//  MyNewBase
//
//  Created by 胡超 on 16/3/9.
//  Copyright © 2016年 lbanma. All rights reserved.
//

#import "ScanViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "QRCodeController.h"

@interface ScanViewController ()<AVCaptureMetadataOutputObjectsDelegate>

//@property ( strong , nonatomic ) AVCaptureDevice * device;
//
//@property ( strong , nonatomic ) AVCaptureDeviceInput * input;
//
//@property ( strong , nonatomic ) AVCaptureMetadataOutput * output;
//
//@property ( strong , nonatomic ) AVCaptureSession * session;
//
//@property ( strong , nonatomic ) AVCaptureVideoPreviewLayer * preview;
//
@end

@implementation ScanViewController


-(instancetype)init{
    if (self = [super init]) {
        self.title = @"扫描";
        self.tabBarItem.image = [UIImage imageNamed:@"tabbar_item_selected"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_item_selected_selected"];
    }
    return self;
}

- (void)saoBtnClick:(UIButton *)sender
{
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusNotDetermined || authStatus == AVAuthorizationStatusDenied){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"相机权限受限" message:@"请在iPhone的\"设置->隐私->相机\"选项中,允许\"夜宵\"访问您的相机." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    QRCodeController *qrcodeVC = [[QRCodeController alloc] init];
    qrcodeVC.view.alpha = 0;
//    AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [self.window.rootViewController addChildViewController:qrcodeVC];
//    [del.window addSubview:qrcodeVC.view];
    [self addChildViewController:qrcodeVC];
    [self.view addSubview:qrcodeVC.view];
    //[self.navigationController popToViewController:qrcodeVC animated:YES];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        qrcodeVC.view.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *bcIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH)];
    bcIV.image = [UIImage imageNamed:@"bc.jpg"];
    
    [self.view addSubview:bcIV];
    [bcIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((kWindowW-200)/2, (kWindowW-200)/2+70, 200, 80)];
    label.font = [UIFont systemFontOfSize:20];
    label.textAlignment = 1;
    label.numberOfLines = 0;
    label.textColor = [UIColor blackColor];
    label.text = @"看什么看，点我才能扫描啊";
    [self.view addSubview:label];
    
    
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"qw" ofType:@"gif"]] ];
    FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
    imageView.animatedImage = image;
    imageView.frame = CGRectMake((kWindowW-200)/2, (kWindowH-200)/2, 200.0, 200.0);
    [self.view addSubview:imageView];
    
    UIButton *buttonn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonn addTarget:self action:@selector(saoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    buttonn.backgroundColor = [UIColor clearColor];
    buttonn.frame = CGRectMake((kWindowW-200)/2, (kWindowH-200)/2, 200.0, 200.0);
//    buttonn.frame = imageView.frame;
    
    [self.view addSubview:buttonn];
    // Do any additional setup after loading the view.
    
    /*
    // Device
    
    _device = [ AVCaptureDevice defaultDeviceWithMediaType : AVMediaTypeVideo ];
    
    // Input
    
    _input = [ AVCaptureDeviceInput deviceInputWithDevice : self . device error : nil ];
    
    // Output
    
    _output = [[ AVCaptureMetadataOutput alloc ] init ];
    
    [ _output setMetadataObjectsDelegate : self queue : dispatch_get_main_queue ()];
    
    [ _output setRectOfInterest : CGRectMake (0.5,0,0.5,1)];
    
    // Session
    
    _session = [[ AVCaptureSession alloc ] init ];
    
    [ _session setSessionPreset : AVCaptureSessionPresetHigh ];
    
    if ([ _session canAddInput : self . input ])
        
    {
        
        [ _session addInput : self . input ];
        
    }
    
    if ([ _session canAddOutput : self . output ])
        
    {
        
        [ _session addOutput : self . output ];
        
    }
    
    // 所有类型 二维码 条形码 ...
    
    _output . metadataObjectTypes = @[ AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code ] ;
    
    // Preview
    
    _preview =[ AVCaptureVideoPreviewLayer layerWithSession : _session ];
    
    _preview . videoGravity = AVLayerVideoGravityResizeAspectFill ;
    
    _preview . frame = self . view . layer . bounds ;
    
    [ self . view . layer insertSublayer : _preview atIndex : 0 ];
    
    // Start
    
    [ _session startRunning ];
     */
}
//#pragma mark AVCaptureMetadataOutputObjectsDelegate
//
//- ( void )captureOutput:( AVCaptureOutput *)captureOutput didOutputMetadataObjects:( NSArray *)metadataObjects fromConnection:( AVCaptureConnection *)connection
//
//{
//    
//    NSString *stringValue;
//    
//    if ([metadataObjects count ] > 0 )
//        
//    {
//        
//        // 停止扫描
//        
//        //[ _session stopRunning ];
//        
//        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
//        
//        stringValue = metadataObject. stringValue ;
//        
//        [self showSuccessMsg:stringValue];
//        
//    }
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
